﻿using HslCommunication.Core;
using PLC.Domain.Dto;
using PLC.Domain.Enum;
using PLC.Domain.Models;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.ScheduledTasks.Strategy.Read;

public class ReadConnectionStatus:BaseRead
{
    private readonly IPLCRepository _plcRepository;

    public ReadConnectionStatus(IPLCRepository plcRepository)
    {
        _plcRepository = plcRepository?? throw new ArgumentNullException(nameof(plcRepository));
    }
    public override EnumReadGroup ReadGroup { get; protected set; } = EnumReadGroup.通讯测试;
    public override void Read(IReadWriteNet readWriteNet, PLCConfig plcConfig, List<DtoDataConfig> readConfigList, PLCReadModel plcReadModel)
    {

        var config = readConfigList?.FirstOrDefault();
        if (config == null)
        {
            throw new Exception("通讯测试的读取参数没有配置");
        }
        // 获取通讯测试的值
        // 规则是  为1的时候 我置为0   为0的时候 哦置为1
        var result =  _plcRepository.ReadData(readWriteNet, config.Register, (EnumDataType)config.DataType,
            (EnumDataEncoding)(config.Encoding ?? 0), config.DataLength);
        ArgumentNullException.ThrowIfNull(result);
        plcReadModel.TestConnection = result;
    }
}